package com.javaweb.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.javaweb.base.BaseDao;
import com.javaweb.base.po.District;
import com.javaweb.constant.CommonConstant;
import com.javaweb.db.query.QueryWapper;
import com.javaweb.util.core.ObjectOperateUtil;
import com.javaweb.util.core.StringUtil;
import com.javaweb.web.eo.roleRestrict.ProvinceCityDistrictAllResponse;
import com.javaweb.web.eo.roleRestrict.ProvinceCityDistrictResponse;
import com.javaweb.web.eo.roleRestrict.RoleRestrictResponse;
import com.javaweb.web.service.DistrictService;

/**
 * 区模块业务层接口实现
 * 
 * @author tank2140896
 */
@Service("districtServiceImpl")
public class DistrictServiceImpl extends BaseDao implements DistrictService {
	
	/**
	 * 获取区列表业务层接口实现
	 * 
	 * @param cityCode 市代码
	 * @param roleRestrictResponse 角色限定请求参数
	 * @return List<ProvinceCityDistrictResponse> 区列表
	 */
	@Override
	public List<ProvinceCityDistrictResponse> getDistrictList(String cityCode,RoleRestrictResponse roleRestrictResponse) {
		List<ProvinceCityDistrictAllResponse> list = roleRestrictResponse.getList();
		List<District> sourceList = new ArrayList<>();
		if(list.size()>0){
			for(int i=0;i<list.size();i++){
				QueryWapper<District> queryWapper = new QueryWapper<>();
				if(!CommonConstant.EMPTY_VALUE.equals(StringUtil.handleNullString(cityCode))){
					queryWapper.eq(District::getCityCode,cityCode);
				}
				ProvinceCityDistrictAllResponse pcdar = list.get(i);
				if("all".equals(pcdar.getCity())){
					sourceList = districtDao.selectList(queryWapper);
					break;
				}else{
					District district = districtDao.selectOne(queryWapper.eq(District::getCode,pcdar.getCity()));
					if(district!=null){
						sourceList.add(district);
					}
				}
			}
		}else{
			QueryWapper<District> queryWapper = new QueryWapper<>();
			if(!StringUtil.isEmpty(cityCode)){
				queryWapper.eq(District::getCityCode,cityCode);
			}
			sourceList = districtDao.selectList(queryWapper);
		}
		List<ProvinceCityDistrictResponse> targetList = ObjectOperateUtil.copyListProperties(sourceList,ProvinceCityDistrictResponse.class);
		return targetList;
	}

	/**
	 * 根据市和区代码是否能获取区信息业务层接口实现
	 * 
	 * @param cityCode 市代码
	 * @param districtCode 区代码
	 * @return boolean true：可以获得区信息；false：不能获得区信息
	 */
	@Override
	public boolean isExistBySelectCityAndDistrictCode(String cityCode, String districtCode) {
		QueryWapper<District> queryWapper = new QueryWapper<>();
		queryWapper.eq(District::getCityCode,cityCode);
		queryWapper.eq(District::getCode,districtCode);
		List<District> list = districtDao.selectList(queryWapper);
		if(CollectionUtils.isEmpty(list)){
			return false;
		}else {
			return true;
		}
	}

}
